package cn.miao.context;

import cn.miao.hrm.domain.Login;
import com.alibaba.fastjson.JSONObject;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

/**
 * @program: springcloud-hrm
 * @description:
 * @author: MiaoWei
 * @create: 2021-12-06 20:34
 **/

public class LoginContext {

    public static Login getLogin(){
        try {
            SecurityContext context = SecurityContextHolder.getContext();
            //这里就能获取到校验token中的user_name, 这里封装了我们想要的数据
            String principal = (String)context.getAuthentication().getPrincipal();
            return JSONObject.parseObject(principal, Login.class);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }
}
